<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/2/28
 * Time: 15:20
 */

namespace App\Http\Logics\MiniProgram;


use App\Category;
use App\Http\Logics\BaseLogic;
class CategoryLogic extends BaseLogic
{
    public static function getCategoryList($data)
    {
        $category = Category::with(['category', 'tag', 'tag.tag'])->where(function ($query) use ($data){
            $query->where('parent_id', $data['parent_id']);
            if (isset($data['status']) && $data['status'] != ''){
                $query->where('status', $data['status']);
            }
        })->orderBy('sort')->get();
        if (empty($category)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $category->map(function ($category) {
            return $category->append('status_bool');
        });
        return self::response(self::SUCCESS, $category);
    }

    public static function getBrotherCategoryList($data)
    {
        $parent_id = Category::select('parent_id')->find($data['category_id'])->parent_id;
        $category = Category::where('parent_id', $parent_id)->get();
        if (empty($category)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $category);
    }
}